/********************************************************
    Copyright (C), 2020-2099,SZ-STREAMING Tech. Co., Ltd.
    File name: V2xMessageEncode.c
    Author: xuzhibin 
    Date: 2020-06-12
    Description: V2X消息帧解码
********************************************************/
#include "V2xMessageEncode.h"

static int RM_SDK_V2X_EncodeCb(const void *buffer, size_t size, void *op_key)
{
    V2xMsgEncodeBuf_t *msgBuf = (V2xMsgEncodeBuf_t *)op_key;
    
    if ((buffer == NULL) || (op_key == NULL))
    {
        V2X_LOG_ERROR("data[0x%x], op_key[0x%x]\n", buffer, op_key);
        return E_V2X_RET_FAIL;
    }

    if(msgBuf->realSize > msgBuf->maxSize)
    {
        V2X_LOG_ERROR("buffer is too small, maxSize[%d] realSize[%d]\n", msgBuf->maxSize, msgBuf->realSize);
        return E_V2X_RET_FAIL;
    }

    memcpy(msgBuf->buffer + msgBuf->realSize, buffer, size);
    msgBuf->realSize += size;

    return E_V2X_RET_SUCC;   
}

/* 消息帧编码接口, msgFram为消息帧指针, msgBuf为编码后的数据 */
int RM_SDK_V2X_MsgFrameEncode(MessageFrame_t *msgFrame, V2xMsgEncodeBuf_t* msgBuf)
{
    asn_enc_rval_t rval;
    
    rval = uper_encode(&asn_DEF_MessageFrame, msgFrame, RM_SDK_V2X_EncodeCb, msgBuf);
    if(rval.encoded == -1) {
        V2X_LOG_ERROR("Could not encode msgFrame (at %s)\n",
        rval.failed_type ? rval.failed_type->name : "unknown");
        return E_V2X_RET_FAIL;
    }
    
    xer_fprint(stdout, &asn_DEF_MessageFrame, msgFrame);

    return E_V2X_RET_SUCC;    
}

/* BSM消息编码接口, bsm为消息帧指针, msgBuf为编码后的数据 */
int RM_SDK_V2X_BsmEncode(BSM_t *bsm, V2xMsgEncodeBuf_t* msgBuf)
{
    asn_enc_rval_t rval;
    
    rval = uper_encode(&asn_DEF_BSM, bsm, RM_SDK_V2X_EncodeCb, msgBuf);
    if(rval.encoded == -1) {
        V2X_LOG_ERROR("Could not encode bsm (at %s)\n",
        rval.failed_type ? rval.failed_type->name : "unknown");
        return E_V2X_RET_FAIL;
    }
    
    //xer_fprint(stdout, &asn_DEF_BSM, bsm);

    return E_V2X_RET_SUCC;    
}

/* MAP消息编码接口, bsm为消息帧指针, msgBuf为编码后的数据 */
int RM_SDK_V2X_MapEncode(MAP_t *map, V2xMsgEncodeBuf_t* msgBuf)
{
    asn_enc_rval_t rval;
    
    rval = uper_encode(&asn_DEF_MAP, map, RM_SDK_V2X_EncodeCb, msgBuf);
    if(rval.encoded == -1) {
        V2X_LOG_ERROR("Could not encode map (at %s)\n",
        rval.failed_type ? rval.failed_type->name : "unknown");
        return E_V2X_RET_FAIL;
    }
    
    //xer_fprint(stdout, &asn_DEF_MAP, map);

    return E_V2X_RET_SUCC;    
}

/* RSM消息编码接口, rsm为消息帧指针, msgBuf为编码后的数据 */
int RM_SDK_V2X_RsmEncode(RSM_t *rsm, V2xMsgEncodeBuf_t* msgBuf)
{
    asn_enc_rval_t rval;
    
    rval = uper_encode(&asn_DEF_RSM, rsm, RM_SDK_V2X_EncodeCb, msgBuf);
    if(rval.encoded == -1) {
        V2X_LOG_ERROR("Could not encode rsm (at %s)\n",
        rval.failed_type ? rval.failed_type->name : "unknown");
        return E_V2X_RET_FAIL;
    }
    
    //xer_fprint(stdout, &asn_DEF_RSM, rsm);

    return E_V2X_RET_SUCC;    
}

/* SPAT消息编码接口, spat为消息帧指针, msgBuf为编码后的数据 */
int RM_SDK_V2X_SpatEncode(SPAT_t *spat, V2xMsgEncodeBuf_t* msgBuf)
{
    asn_enc_rval_t rval;
    
    rval = uper_encode(&asn_DEF_SPAT, spat, RM_SDK_V2X_EncodeCb, msgBuf);
    if(rval.encoded == -1) {
        V2X_LOG_ERROR("Could not encode spat (at %s)\n",
        rval.failed_type ? rval.failed_type->name : "unknown");
        return E_V2X_RET_FAIL;
    }
    
    //xer_fprint(stdout, &asn_DEF_SPAT, spat);

    return E_V2X_RET_SUCC;    
}

/* RSI消息编码接口, rsi为消息帧指针, msgBuf为编码后的数据 */
int RM_SDK_V2X_RsiEncode(RSI_t *rsi, V2xMsgEncodeBuf_t* msgBuf)
{
    asn_enc_rval_t rval;
    
    rval = uper_encode(&asn_DEF_RSI, rsi, RM_SDK_V2X_EncodeCb, msgBuf);
    if(rval.encoded == -1) {
        V2X_LOG_ERROR("Could not encode rsi (at %s)\n",
        rval.failed_type ? rval.failed_type->name : "unknown");
        return E_V2X_RET_FAIL;
    }
    
    //xer_fprint(stdout, &asn_DEF_RSI, rsi);

    return E_V2X_RET_SUCC;    
}

